﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TaobaoDistrSales.Common;
using TaobaoDistrSales.Model;
using System.Threading;

namespace TaobaoDistrSales
{
    public partial class MainDashBoardControl : UserControl
    {
        protected Mutex statusMutex = new Mutex();
        protected long TotalSuccesCount = 0;
        protected DateTime startDate = DateTime.Now;
        #region Ctor

        public MainDashBoardControl()
        {
            InitializeComponent(); 
        }

        #endregion

        #region Properties

        public TaobaoProcessor TaobaoProcessor
        {
            get
            {
                return taobaoProcessor;
            }
            set
            {
                taobaoProcessor = value;
                if (value != null)
                {
                    taobaoProcessor.UpdateStatus = (status) =>
                    {                        
                        if (CurrentActionLabel.InvokeRequired)
                            CurrentActionLabel.Invoke(new Action<string>(s => {
                                CurrentActionLabel.Text = s;
                            }), status);
                         
                    };
                    taobaoProcessor.ProcessStarted += new ProcessStartedEventHanlder(taobaoProcessor_ProcessStarted);
                    taobaoProcessor.ProcessCompleted += new ProcessCompletedEventHandler(taobaoProcessor_ProcessCompleted);
                    taobaoProcessor.ShopChanged += new Action<Shop>(taobaoProcessor_ShopChanged);
                }
            }
        }

        void taobaoProcessor_ShopChanged(Shop obj)
        {
            try
            {
                statusMutex.WaitOne();
                if (CurrentShopLabel.InvokeRequired)
                {
                    CurrentShopLabel.Invoke(new Action<Shop>(
                        shop =>
                        {
                            CurrentShopLabel.Text = shop.Name;
                        }
                        ), obj);
                }
                else
                {
                    CurrentShopLabel.Text = obj.Name;
                }
                statusMutex.ReleaseMutex();
            }
            catch { }
            try
            {
                statusMutex.WaitOne();
                if (this.StartTimeLabel.InvokeRequired)
                {
                    StartTimeLabel.Invoke(new Action<Shop>(
                        shop =>
                        {
                            StartTimeLabel.Text = "等待开始...";
                        }
                        ), obj);
                }
                else
                {
                    StartTimeLabel.Text = "等待开始...";
                }
                statusMutex.ReleaseMutex();
            }
            catch { }
        }

        void taobaoProcessor_ProcessCompleted(object sender, TaobaoProcessorEventArgs e)
        {
            try
            {
                if (TotalSuccesCount + 1 < long.MaxValue)
                    TotalSuccesCount++;
                else
                    TotalSuccesCount = 0;

                statusMutex.WaitOne();
                if (this.StartTimeLabel.InvokeRequired)
                {
                    StartTimeLabel.Invoke(new Action<TaobaoProcessorEventArgs>(
                        tpe =>
                        {
                            StartTimeLabel.Text = DateTime.Now.ToString() + "完成";
                        }
                        ), e);
                }
                else
                {
                    StartTimeLabel.Text = DateTime.Now.ToString() + "完成";
                }
                statusMutex.ReleaseMutex();
            }
            catch { } 
            try
            {
                statusMutex.WaitOne();
                if (this.TotalCountLabel.InvokeRequired)
                {
                    TotalCountLabel.Invoke(new Action<TaobaoProcessorEventArgs>(
                        tpe =>
                        {
                            TotalCountLabel.Text = "自" + startDate.ToString() + "起，" + TotalSuccesCount + " 次";
                        }
                        ), e);
                }
                else
                {
                    TotalCountLabel.Text = "自" + startDate.ToString() + "起，" + TotalSuccesCount + " 次";
                }
                statusMutex.ReleaseMutex();
            }
            catch { }
            try
            {
                statusMutex.WaitOne();
                if (this.LastCompletedTimeLabel.InvokeRequired)
                {
                    LastCompletedTimeLabel.Invoke(new Action<TaobaoProcessorEventArgs>(
                        tpe =>
                        {
                            LastCompletedTimeLabel.Text =  tpe.ShopName + ",结束于" + DateTime.Now.ToString();
                        }
                        ), e);
                }
                else
                {
                    LastCompletedTimeLabel.Text =  e.ShopName + ",结束于" + DateTime.Now.ToString();
                }
                statusMutex.ReleaseMutex();
            }
            catch { }
        }

        void taobaoProcessor_ProcessStarted(object sender, TaobaoProcessorEventArgs e)
        {
            try
            {
                statusMutex.WaitOne();
                if (CurrentShopLabel.InvokeRequired)
                {
                    CurrentShopLabel.Invoke(new Action<TaobaoProcessorEventArgs>(
                        tpe =>
                        {
                            CurrentShopLabel.Text = tpe.ShopName;
                        }
                        ), e);
                }
                else
                {
                    CurrentShopLabel.Text = e.ShopName;
                }
                statusMutex.ReleaseMutex();
            }catch{}
            try
            {
                statusMutex.WaitOne();
                if (this.StartTimeLabel.InvokeRequired)
                {
                    StartTimeLabel.Invoke(new Action<TaobaoProcessorEventArgs>(
                        tpe =>
                        {
                            StartTimeLabel.Text = DateTime.Now.ToString() + "开始";
                        }
                        ), e);
                }
                else
                {
                    StartTimeLabel.Text = DateTime.Now.ToString() + "开始";
                }
                statusMutex.ReleaseMutex();
            }
            catch { }
        }
                

        private TaobaoProcessor taobaoProcessor = null;

        #endregion

        #region Load

        private void MainDashBoardControl_Load(object sender, EventArgs e)
        {
            startDate = DateTime.Now;
            TotalCountLabel.Text ="自"+startDate.ToString()+"起，"+ TotalSuccesCount + " 次";
            
            using (taobaodbEntities entities = ResourceManager.LoadEntity())
            {
                OptionsConfig config = entities.OptionsConfig.FirstOrDefault();
                OutputTextBox.Visible = config != null ? config.showlog : true;
            }
        }

        #endregion

        #region Button Events

        private void StartButton_Click(object sender, EventArgs e)
        {
            if (TaobaoProcessor.OutputWriter == null)
            {
                TaobaoProcessor.OutputWriter = new TextBoxWriter(OutputTextBox);
            }
            if ( (TaobaoProcessor.Status == ProcessStatus.None||TaobaoProcessor.Status== ProcessStatus.Stoped) && StartButton.Text =="开始")
            {
                TaobaoProcessor.Start();
                StartButton.Text = "暂停";
            }
            else if ((TaobaoProcessor.Status == ProcessStatus.Started || TaobaoProcessor.Status == ProcessStatus.Resumed) && StartButton.Text == "暂停")
            {
                TaobaoProcessor.Suspend();
                StartButton.Text = "继续";
            }
            else if (TaobaoProcessor.Status == ProcessStatus.Suspended && StartButton.Text == "继续")
            {
                TaobaoProcessor.Resume();
                StartButton.Text = "暂停";
            }

        }
        
        private void ClearButton_Click(object sender, EventArgs e)
        {            
            TaobaoProcessor.Clear();

        }


        #endregion


        #region Assist Methods
        
        public void UpdateStatus(string status)
        {
            CurrentActionLabel.Text = status;
        }
        
        #endregion
         

        #region Dispose
         

        #endregion
    }
}
